package mywebapps.security.jsf;

import javax.annotation.PostConstruct;

import mywebapps.common.jsf.BaseAction;
import mywebapps.security.dao.UserDao;
import mywebapps.security.model.User;

public class CreateUserAction extends BaseAction
{
    private UserDao userDao;
    private User user;
    private boolean usernameAvailable;
    private boolean usernameValidationPerformed;
    
    @PostConstruct
    public void init()
    {
        user = new User();
    }
    
    public String createUser()
    {
        userDao.createUser(user);
        return "gotoViewUsers";
    }
    
    public void checkUsernameAvailability()
    {
        User existingUser = userDao.getUser(user.getUsername());
        if (existingUser != null) {
            usernameAvailable = false;
        } else {
            usernameAvailable = true;
        }
        
        usernameValidationPerformed = true;
    }

    public void setUserDao(UserDao userDao)
    {
        this.userDao = userDao;
    }

    public User getUser()
    {
        return user;
    }

    public boolean isUsernameAvailable()
    {
        return usernameAvailable;
    }

    public boolean isUsernameValidationPerformed()
    {
        return usernameValidationPerformed;
    }
}
